package com.novellotus.iov.carvice.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.ResourcesManager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ChargeStationDetailActivity extends BaseActivity {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.btn_navigation)
    Button btn_navigation;
    @BindView(R.id.view_bottom)
    View view_bottom;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_charge_station_detail);
    }

    @Override
    public void init() {
        tv_tittle.setText("充电站详情");
    }


    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_share)
    void share() {
        //onOneKeyShare();
        showShare();
    }


    private void showShare() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_share, null);
        RelativeLayout rlt_weixin = view.findViewById(R.id.rlt_weixin);
        rlt_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                weixin();
            }
        });
        RelativeLayout rlt_friend = view.findViewById(R.id.rlt_friend);
        rlt_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                friend();
            }
        });
        RelativeLayout rlt_qq = view.findViewById(R.id.rlt_qq);
        rlt_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qq();
            }
        });
        PopupWindow popupWindow = new PopupWindow();
        popupWindow.setContentView(view);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 允许点击外部消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());//注意这里如果不设置，下面的setOutsideTouchable(true);允许点击外部消失会失效
        popupWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        popupWindow.setFocusable(true);
        // popupWindow.show(top,0,0);
        //显示在上方
        //获取自身的长宽高
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupHeight = view.getMeasuredHeight();
        int popupWidth = view.getMeasuredWidth();
        int[] location = new int[2];
        view_bottom.getLocationOnScreen(location);
        // popupWindow.showAtLocation(btn_navigation, Gravity.NO_GRAVITY, location[0] + btn_navigation.getWidth() / 2, location[1] - view.getHeight());
        //显示在正上方
        //在控件上方显示
        MyViewUtils.backgroundAlpha(this, 0.5f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                MyViewUtils.backgroundAlpha(ChargeStationDetailActivity.this, 1f);
            }
        });
        popupWindow.showAtLocation(view_bottom, Gravity.LEFT, 0, location[1] - popupHeight);
    }


    /**
     * 分享微信朋友圈
     */
    private void weixin() {

        Platform.ShareParams wechat = new Platform.ShareParams();
        wechat.setTitle("share sdk");
        wechat.setText("share");
        //    wechat.setImageUrl(logo);
        wechat.setUrl("www.baidu.com");
        wechat.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(Wechat.NAME);
        weixin.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                MyUtils.logInformation("完成");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        weixin.share(wechat);
    }

    /**
     * 分享微信朋友圈
     */
    private void friend() {
        Platform.ShareParams friend = new Platform.ShareParams();
        friend.setTitle("share sdk");
        friend.setText("share");
        //    wechat.setImageUrl(logo);
        friend.setUrl("www.baidu.com");
        friend.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(WechatMoments.NAME);
        weixin.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                MyUtils.logInformation("完成");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        weixin.share(friend);
    }

    /**
     * 分享qq
     */
    private void qq() {
        OnekeyShare oks = new OnekeyShare();
        oks.setImageUrl("http://firicon.fir.im/baa18a6d779c597888d685f1159070df5b4f2912");
        oks.setTitleUrl("http://www.baidu.com");
        oks.setText("你们好");
        oks.setTitle("mother fuck ");
        oks.setPlatform(QQ.NAME);
        //oks.setPlatform(QZone.NAME);
        oks.show(this);
    }

    /**
     * 一键分享
     */
    private void onOneKeyShare() {
        cn.sharesdk.onekeyshare.OnekeyShare oks = new cn.sharesdk.onekeyshare.OnekeyShare();
        oks.setAddress("12345678901");
        ResourcesManager manager = ResourcesManager.getInstace(this);
        if (!TextUtils.isEmpty(manager.getFilePath())) {
            oks.setFilePath(manager.getFilePath());
        } else {
            oks.setFilePath(manager.getFilePath());
        }
        oks.setTitle(manager.getTitle());
        oks.setTitleUrl(manager.getTitleUrl());
        oks.setUrl(manager.getUrl());
        oks.setMusicUrl(manager.getMusicUrl());
        String customText = manager.getText();
        if (customText != null) {
            oks.setText(customText);
        } else if (manager.getText() != null && manager.getText().contains("0")) {
            oks.setText(manager.getText());
        } else {
            oks.setText(this.getString(R.string.share_content));
        }
        oks.setComment(manager.getComment());
        oks.setSite(manager.getSite());
        oks.setSiteUrl(manager.getSiteUrl());
        oks.setVenueName(manager.getVenueName());
        oks.setVenueDescription(manager.getVenueDescription());
        oks.setSilent(true);
        oks.setLatitude(23.169f);
        oks.setLongitude(112.908f);
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " completed at " + msg;
                Message message = new Message();
                message.obj = text;
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + "caught error at " + msg;
                Message message = new Message();
                message.obj = text;
            }

            @Override
            public void onCancel(Platform platform, int i) {
                String msg = ResourcesManager.actionToString(i);
                String text = platform.getName() + " canceled at " + msg;
                Message message = new Message();
                message.obj = text;
            }
        });
        oks.show(this);
    }
}
